<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-08 09:40
 *
 * 项目：upload  -  $  - ModulesHelper.php
 *
 * 作者：liwei 
 */


namespace lev\helpers;

use Lev;
use lev\base\Modulesv;
use lev\widgets\inputs\inputsWidget;
use modules\levmodules\table\form\Form_lev_modules\Form_lev_modules;

!defined('INLEV') && exit('Access Denied LEV');

class ModulesHelper extends LevModulesREADME
{

    public static function formatModuleInfo($data = []) {
        unset($data['id'], $data['typeid'], $data['identifier'], $data['versiontime'], $data['uptime'], $data['addtime']);
        $modInfo = ModulesHelper::getModuleInfo(Lev::$app['iden']);
        if ($modInfo) {
            unset($data['name'], $data['copyright'], $data['descs'], $data['displayorder']);
        }
        $data = dbHelper::safeColumns(static::tableName(), $data);
        $data['identifier'] = Lev::$app['iden'];
        $data['uptime'] = Lev::$app['timestamp'];
        if (isset($data['copyright'])) {
            if (stripos($data['identifier'], 'lev') === 0) {
                $data['typeid'] = 8;
            }
        }
        return $data;
    }

    /**
     * @param string $optab
     * @param array $upData
     * @return array
     */
    public static function setStatus($optab = '', $upData = [])
    {
        $ck = parent::setStatus($optab, $upData); // TODO: Change the autogenerated stub
        if ($ck && $ck['status'] >0) {
            static::createModuleFile(static::findOne(['id'=>intval(Lev::GPv('opid'))]));
        }
        return $ck;
    }

    public static function setField($optab = '', $val = null, $upData = [])
    {
        $field = Lev::stripTags(Lev::GPv('field'));
        if ($field == 'version') {
            $val = $val ?: Lev::stripTags(Lev::GPv('val'));
            if ($errMsg = Form_lev_modules::isErrorVersion($val)) {
                return $errMsg;
            }
        }

        $upData['versiontime'] = Lev::$app['timestamp'];
        return parent::setField($optab, $val, $upData); // TODO: Change the autogenerated stub
    }

    public static function setCaches() {
        $data = static::findAll(1);
        if ($data) {
            foreach ($data as $v) {
                static::createModuleFile($v);
            }
        }
    }

    public static function adminop($adminop)
    {
        $tips = parent::adminop($adminop); // TODO: Change the autogenerated stub
        if (isset($tips)) {
            inputsWidget::setCaches();
            ModulesHelper::setCaches();
        }
        return $tips;
    }

    /**
     * 获取未安装组件
     * @return array
     */
    public static function getInstallModules() {
        return Modulesv::getInstallModules();
    }
    public static function getRouteFile($iden, $classdir) {
        return Modulesv::getRouteFile($iden, $classdir);
    }

    public static function createModuleFile($mudInfo) {
        return Modulesv::createModuleFile($mudInfo);
    }

    public static function deleteModuleFile($iden) {
        Modulesv::deleteModuleFile($iden);
    }

}